package com.transmit.host;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

public class FilBrowserActivity extends Activity implements OnClickListener{

	private static final int row = 4;
	
	private final int CONFIRM = 1;
	private final int CANCEL = 2;
	// private String filePath;
	private TextView textView;
	private Button confirm, cancel;
	private List<String> pathList;
	private List<String> nameList;
	private GridView gridView;
	private FileAdapter adapter;
	private int width;
	private int height;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browser);
		width= this.getResources().getDisplayMetrics().widthPixels / row;
		height = width + width / 5;
		String SDPATH = Environment.getExternalStorageDirectory()
				.getAbsolutePath();

		textView = (TextView) findViewById(R.id.pathtextview);
		gridView = (GridView) findViewById(R.id.gridView1);
		confirm = (Button)findViewById(R.id.comfirm);
		cancel = (Button)findViewById(R.id.cancel);
		
		Intent intent = this.getIntent();
		
		initListener();
		showGridView(intent.getStringExtra("path"));
		
	}
	
	private void initListener(){
		confirm.setOnClickListener(this);
		cancel.setOnClickListener(this);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				showGridView(pathList.get(position));
			}
		});
	}

	public void showGridView(String filePath) {
		textView.setText(filePath);
		pathList = new ArrayList<String>();
		nameList = new ArrayList<String>();
		File f = new File(filePath);
		Log.i("file", f.getName() + "---f first");
		if (f.exists() && f.canWrite()) {
			// Log.i("file", f.getName() + "---f first");
			File[] files = f.listFiles();
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				if (file.isDirectory()) {
					nameList.add(file.getName());
					pathList.add(file.getPath());
					// Log.i("file", file.getName() + "");
				}
			}
			Log.i("file", nameList.toString() + "");
		}
		adapter = new FileAdapter(getLayoutInflater(), nameList, width, height);
		gridView.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == confirm){
			Intent data = new Intent();
			data.putExtra("path", textView.getText());
			FilBrowserActivity.this.setResult(CONFIRM, data);
			finish();
		}else if(v == cancel){
			FilBrowserActivity.this.setResult(CANCEL, null);
			finish();
		}
			
	}
	
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        	StringBuffer sb = new StringBuffer(textView.getText());
        	int temp = sb.lastIndexOf("/");
        	String judge = sb.substring(temp + 1 , sb.length());
        	String prePath = sb.substring(0, temp);
        	
        	Log.i("tag", "prePath--->" + prePath);
        	if(judge.equals("sdcard")){
        		FilBrowserActivity.this.setResult(CANCEL, null);
        		finish();
        	}else{
        		showGridView(prePath);
        	}
        }
		return false;
	}
}
